library(plyr)
library(xtable)
library(apsrtable)
library(sandwich)

load('traffic.survey.RData')
load('survey2.RData')
load('survey3.RData')

###
#Survey 1 (Web Traffic and Survey from 2018)
###

survey1 <- traffic.survey
survey1 <- subset(survey1,!is.na(survey1$espn.trust))
survey1$political.interest[which(survey1$political.interest=="Don't know")] <- 'Hardly at all'
survey1$political.interest <- as.character(survey1$political.interest)
survey1$race <- mapvalues(x=survey1$race,from=c('White','Black','Hispanic','Asian','Native American','Mixed','Other','Middle Eastern'),to=c('White','Black','Hispanic','Asian','Other','Other','Other','Other'))
survey1$race <- as.character(survey1$race)
survey1$education <- as.character(survey1$education)

#Preparing Demographic Information
survey1$republican <- as.numeric(as.character(mapvalues(x=as.character(survey1$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(0,0,1))))
survey1$democrat <- as.numeric(as.character(mapvalues(x=as.character(survey1$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(1,0,0))))
survey1$independent <- as.numeric(as.character(mapvalues(x=as.character(survey1$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(0,1,0))))
survey1$female <- as.numeric(as.character(mapvalues(x=survey1$gender,from=c('Male','Female'),to=c(0,1))))
survey1$white <- as.numeric(as.character(mapvalues(x=survey1$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(1,0,0,0,0))))
survey1$hispanic <- as.numeric(as.character(mapvalues(x=survey1$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,0,1,0,0))))
survey1$black <- as.numeric(as.character(mapvalues(x=survey1$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,1,0,0,0))))
survey1$other.race <- as.numeric(as.character(mapvalues(x=survey1$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,0,0,1,1))))
survey1$college <- as.numeric(as.character(mapvalues(x=survey1$education,from=c('No HS','High school graduate','Some college','2-year','4-year','Post-grad'),to=c(0,0,0,0,1,1))))

survey1.full.demos <- survey1[,c('democrat','independent','republican','age','female','white','hispanic','black','other.race','college','weight')]
survey1.full.demos$survey <- 'Survey 1'

survey1.traffic.demos <- survey1[which(survey1$web.traffic.available==1 & survey1$espn.survey==1),c('democrat','independent','republican','age','female','white','hispanic','black','other.race','college')]
survey1.traffic.demos$survey <- 'Web Traffic'
survey1.traffic.demos$weight <- 1

survey1.panel.demos <- survey1[which(survey1$panel==1),c('democrat','independent','republican','age','female','white','hispanic','black','other.race','college')]
survey1.panel.demos$survey <- 'Panel'
survey1.panel.demos$weight <- 1

survey1 <- subset(survey1, survey1$espn.survey==1)

###
#Survey 2 (2019 Survey)
###

survey2 <- unique(survey2[,c('rid','espn.liberal','pid_3pt','sports.preference','age','gender','education','race','political.interest')])
survey2$race[which(!(survey2$race %in% c('Asian or Asian-American','Black or African American','Hispanic or Latino','White')))] <- 'Other'
survey2$race <- mapvalues(survey2$race,from=c('Asian or Asian-American','Black or African American','Hispanic or Latino','White','Other'),to=c('Asian','Black','Hispanic','White','Other'))
survey2$race <- as.character(survey2$race)
survey2$age <- as.numeric(as.character(survey2$age))
survey2$education <- mapvalues(survey2$education,from=c('Did not graduate from high school','High School Graduate','Some college, but no degree (yet)','2-year college degree','4-year college degree','Post-Graduate degree (MA, MBA, MD, JD, PhD, etc.)'),to=c('No HS','High school graduate','Some college','2-year','4-year','Post-grad'))
survey2$education <- as.character(survey2$education)
survey2$political.interest <- as.character(survey2$political.interest)
survey2$political.interest[which(survey2$political.interest=="")] <- NA
survey2$gender <- as.character(survey2$gender)
survey2$gender[which(survey2$gender=="")] <- NA

#Preparing Demographic Information
survey2$republican <- as.numeric(as.character(mapvalues(x=as.character(survey2$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(0,0,1))))
survey2$democrat <- as.numeric(as.character(mapvalues(x=as.character(survey2$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(1,0,0))))
survey2$independent <- as.numeric(as.character(mapvalues(x=as.character(survey2$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(0,1,0))))
survey2$female <- as.numeric(as.character(mapvalues(x=survey2$gender,from=c('Male','Female'),to=c(0,1))))
survey2$white <- as.numeric(as.character(mapvalues(x=survey2$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(1,0,0,0,0))))
survey2$hispanic <- as.numeric(as.character(mapvalues(x=survey2$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,0,1,0,0))))
survey2$black <- as.numeric(as.character(mapvalues(x=survey2$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,1,0,0,0))))
survey2$other.race <- as.numeric(as.character(mapvalues(x=survey2$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,0,0,1,1))))
survey2$college <- as.numeric(as.character(mapvalues(x=survey2$education,from=c('No HS','High school graduate','Some college','2-year','4-year','Post-grad'),to=c(0,0,0,0,1,1))))

survey2.full.demos <- survey2[,c('democrat','independent','republican','age','female','white','hispanic','black','other.race','college')]
survey2.full.demos$weight <- 1
survey2.full.demos$survey <- 'Survey 2'

###
#Survey 3 (2020 Survey)
###

survey3 <- unique(survey3[,c('rid','espn.bias','espn.liberal','pid_3pt','sports.preference','age','gender','education','race','political.interest','espntreatment')])
survey3 <- subset(survey3,!is.na(survey3$espn.liberal))
survey3$race[which(!(survey3$race %in% c('Asian or Asian-American','Black or African American','Hispanic or Latino','White')))] <- 'Other'
survey3$race <- mapvalues(survey3$race,from=c('Asian or Asian-American','Black or African American','Hispanic or Latino','White','Other'),to=c('Asian','Black','Hispanic','White','Other'))
survey3$race <- as.character(survey3$race)
survey3$age <- as.numeric(as.character(survey3$age))
survey3$education <- mapvalues(survey3$education,from=c('Did not graduate from high school','High School Graduate','Some college, but no degree (yet)','2-year college degree','4-year college degree','Post-Graduate degree (MA, MBA, MD, JD, PhD, etc.)'),to=c('No HS','High school graduate','Some college','2-year','4-year','Post-grad'))
survey3$education <- as.character(survey3$education)
survey3$political.interest <- as.character(survey3$political.interest)
survey3$political.interest[which(survey3$political.interest=="")] <- NA
survey3$gender <- as.character(survey3$gender)
survey3$gender[which(survey3$gender=="")] <- NA

survey3$republican <- as.numeric(as.character(mapvalues(x=as.character(survey3$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(0,0,1))))
survey3$democrat <- as.numeric(as.character(mapvalues(x=as.character(survey3$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(1,0,0))))
survey3$independent <- as.numeric(as.character(mapvalues(x=as.character(survey3$pid_3pt),from=c('Democrat','Independent','Republican'),to=c(0,1,0))))
survey3$female <- as.numeric(as.character(mapvalues(x=survey3$gender,from=c('Male','Female'),to=c(0,1))))
survey3$white <- as.numeric(as.character(mapvalues(x=survey3$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(1,0,0,0,0))))
survey3$hispanic <- as.numeric(as.character(mapvalues(x=survey3$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,0,1,0,0))))
survey3$black <- as.numeric(as.character(mapvalues(x=survey3$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,1,0,0,0))))
survey3$other.race <- as.numeric(as.character(mapvalues(x=survey3$race,from=c('White','Black','Hispanic','Asian','Other'),to=c(0,0,0,1,1))))
survey3$college <- as.numeric(as.character(mapvalues(x=survey3$education,from=c('No HS','High school graduate','Some college','2-year','4-year','Post-grad'),to=c(0,0,0,0,1,1))))

survey3.full.demos <- survey3[,c('democrat','independent','republican','age','female','white','hispanic','black','other.race','college')]
survey3.full.demos$weight <- 1
survey3.full.demos$survey <- 'Survey 3'

###
#Demographic Tables
###

full.surveys <- rbind.data.frame(survey1.full.demos,survey2.full.demos,survey3.full.demos)
web.surveys <- rbind.data.frame(survey1.traffic.demos,survey1.panel.demos)

full.demos <- ddply(full.surveys,.(survey),summarise,black=weighted.mean(x=black,w=weight,na.rm=TRUE),hispanic=weighted.mean(x=hispanic,w=weight,na.rm=TRUE),white=weighted.mean(x=white,w=weight,na.rm=TRUE),other.race=weighted.mean(x=other.race,w=weight,na.rm=TRUE),college=weighted.mean(x=college,w=weight,na.rm=TRUE),female=weighted.mean(x=female,w=weight,na.rm=TRUE),age=weighted.mean(x=age,w=weight,na.rm=TRUE),democrat=mean(x=democrat,w=weight,na.rm=TRUE),republican=mean(x=republican,w=weight,na.rm=TRUE),independent=mean(x=independent,w=weight,na.rm=TRUE),sample.size=length(survey))
full.demos[,2:dim(full.demos)[2]] <- round(full.demos[,2:dim(full.demos)[2]],digits=2)
full.demos <- as.data.frame(t(full.demos))

#Table A1 in Appendix
xtable(full.demos,digits=2,caption="Demographics by Survey")

web.demos <- ddply(web.surveys,.(survey),summarise,black=weighted.mean(x=black,w=weight,na.rm=TRUE),hispanic=weighted.mean(x=hispanic,w=weight,na.rm=TRUE),white=weighted.mean(x=white,w=weight,na.rm=TRUE),other.race=weighted.mean(x=other.race,w=weight,na.rm=TRUE),college=weighted.mean(x=college,w=weight,na.rm=TRUE),female=weighted.mean(x=female,w=weight,na.rm=TRUE),age=weighted.mean(x=age,w=weight,na.rm=TRUE),democrat=mean(x=democrat,w=weight,na.rm=TRUE),republican=mean(x=republican,w=weight,na.rm=TRUE),independent=mean(x=independent,w=weight,na.rm=TRUE),sample.size=length(survey))
web.demos[,2:dim(web.demos)[2]] <- round(web.demos[,2:dim(web.demos)[2]],digits=2)
web.demos <- as.data.frame(t(web.demos))

#Table A2 in Appendix
xtable(web.demos,digits=2,caption="Web Traffic by Survey")

###
#Partisan Differences in Views of ESPN
###

survey3 <- subset(survey3,survey3$espntreatment=='control')

#Unadjusted Differences
diff1 <- lm(espn.trust.standard ~ I(pid_3pt=='Independent') + I(pid_3pt=='Republican'),data=survey1,weights=weight)
diff1$se <- sqrt(diag(vcovHC(diff1,type='HC1')))
diff2 <- lm(espn.liberal ~ I(pid_3pt=='Independent') + I(pid_3pt=='Republican') ,data=survey2)
diff2$se <- sqrt(diag(vcovHC(diff2,type='HC1')))
diff3 <- lm(espn.liberal ~ I(pid_3pt=='Independent') + I(pid_3pt=='Republican'),data=survey3)
diff3$se <- sqrt(diag(vcovHC(diff3,type='HC1')))

#Table 1
apsrtable(diff1,diff2,diff3,coef.names=c("(Intercept)","Independent","Republican"),model.names=c('Trust (Survey 1)','Liberal Bias (Survey 2)','Liberal Bias (Survey 3)'),caption=c('Partisan Divides in Assessments of ESPN'),notes=list('Reference category is Democrats','Robust standard errors in parentheses',stars.note))

#Differences with Covariates
diff1.covariates <- lm(espn.trust.standard ~ I(pid_3pt=='Independent') + I(pid_3pt=='Republican') + political.interest + age + I(gender=='Male') + education + race + other.sports.visits.per.day,data=survey1,weights=weight)
diff1.covariates$se <- sqrt(diag(vcovHC(diff1.covariates,type='HC1')))
diff2.covariates <- lm(espn.liberal ~ I(pid_3pt=='Independent') + I(pid_3pt=='Republican') + political.interest + age + I(gender=='Male') + education + race + sports.preference,data=survey2)
diff2.covariates$se <- sqrt(diag(vcovHC(diff2.covariates,type='HC1')))
diff3.covariates <- lm(espn.liberal ~ I(pid_3pt=='Independent') + I(pid_3pt=='Republican') + political.interest + age + I(gender=='Male') + education + race + sports.preference ,data=survey3)
diff3.covariates$se <- sqrt(diag(vcovHC(diff3.covariates,type='HC1')))

#Appendix Table D1
apsrtable(diff1.covariates,diff2.covariates,diff3.covariates,model.names=c('Trust (Survey 1)','Liberal Bias (Survey 2)','Liberal Bias (Survey 3)'),caption=c('Partisan Divides in Assessments of ESPN (With Covariates)'),notes=list('Reference category is Democrats','Robust standard errors in parentheses',stars.note),coef.names=c("(Intercept)","Independent","Republican","Political Interest-Most of Time","Political Interest-Only now and then","Political Interest-Some of the time","Age","Male","Education-4year","Education-High School","Education-No HS","Education-Postgrad","Education-Some college","Race-Black","Race-Hispanic","Race-Other","Race-White","Sports Website Visits/Day","Sports Interest"))
